import json
from pathlib import Path
from typing import Optional, Dict, Any, List
import re

class JsonlToMarkdown:
    def __init__(self):
        self.md_content = []
        
    def _clean_text(self, text: str) -> str:
        text = re.sub(r'\s+', ' ', text).strip()
        return text
    
    def _process_json_content(self, content: Dict[str, Any]) -> None:
        if 'text' in content:
            text = self._clean_text(content['text'])
            if text:
                self.md_content.append(text)
                self.md_content.append('\n')
    
    def convert(self, input_file: str, output_file: Optional[str] = None) -> Optional[str]:
        try:
            if output_file is None:
                output_file = str(Path(input_file).with_suffix('.md'))
            
            self.md_content = []
            
            with open(input_file, 'r', encoding='utf-8') as f:
                for line in f:
                    try:
                        content = json.loads(line.strip())
                        self._process_json_content(content)
                    except json.JSONDecodeError:
                        print(f"警告：跳过无效的 JSON 行: {line.strip()}")
                        continue
            
            with open(output_file, 'w', encoding='utf-8') as f:
                f.write('\n'.join(self.md_content))
            
            print(f"转换成功：{output_file}")
            return str(output_file)
            
        except Exception as e:
            print(f"转换失败：{str(e)}")
            return None

# 使用示例
if __name__ == '__main__':
    converter = JsonlToMarkdown()
    input_file = '/data/hyq/project/olmocr/localworkspace/results/财务报表.jsonl'
    output_file = '/data/hyq/project/olmocr/localworkspace/results/财务报表.md'
    converter.convert(input_file, output_file)
